<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="经办人">
              <!-- <a-input placeholder="请输入经办人" v-model="queryParam.agent"></a-input> -->
              <j-dict-select-tag type="list" v-model="queryParam.agent" dictCode="oil_card_subsidy_agent" placeholder="请选择经办人"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="领用人">
              <!-- <a-input placeholder="请输入领用人" v-model="queryParam.cardRecipient"></a-input> -->
              <select-user-by-dep   :multi="false" v-model="queryParam.cardRecipient"></select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" :disabled="isDisabledAuth('erpOilCardManager:add')">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('油补维护表')" :disabled="isDisabledAuth('erpOilCardManager:export')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import" :disabled="isDisabledAuth('erpOilCardManager:import')">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <!--<j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" :disabled="isDisabledAuth('erpOilCardManager:delete')"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      </div> -->

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:'110%'}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        
        class="j-table-force-nowrap"
        :customRow="customRow"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
            <!-- <a-button
                class="ruleButton"
                :ghost="true"
                type="primary"
                icon="edit"
                @click="handleEdit(record)"
                :disabled="isDisabledAuth('erpOilCardManager:edit')"
        /> -->
        <!-- <a-divider type="vertical"/> -->
        <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="edit"
                    @click="handleEdit(record)"
                    :disabled="isDisabledAuth('erpCarSubsidy:edit')"
        />
        <a-divider v-if="record.cardStatus != '1' " type="vertical"/>
        
        <!-- todo: 转移按钮弹窗  record.cardTransferStatus != '1' &&    record.cardTransferStatus != '1' && -->
        <a-button 
                    v-if="record.cardStatus != '1'"
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="swap"
                    @click="handleTransfer(record)"
                    :disabled="isDisabledAuth('erpCarSubsidy:transfer')"
        />
        <!-- <a-divider type="vertical" v-if="record.cardTransferStatus != '1'"/>
        <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a-button
                    class="ruleButton"
                    :ghost="true"
                    type="primary"
                    icon="delete"
                    :disabled="isDisabledAuth('erpOilCardManager:delete')"/>
        </a-popconfirm> -->
        </span>

      </a-table>
    </div>

    <a-modal
    :width="1100"
    :bodyStyle ="{
      }"
      title="油卡金额转移"
      :visible="modalvisible"
      @ok="handleOKk"
      @cancel="handleCancel()"
      
    >
        <a-form layout="inline">
          <a-form-item label="旧油卡卡号" required>
            <SelectOilCardNumberInput ref="oldCard" v-model="newNumber" style="width: 200px" :status="'2'" :multi="false"  @back="OldOilbackHandle"></SelectOilCardNumberInput>
          </a-form-item>
          <a-form-item label="截止当前累计自驾补助余额">
            <a-input-number :max="99999999" :min="0"  v-model.number="oldOilCardDrivingSubsidyBalance" placeholder="截止当前累计自驾补助余额" disabled  style="width: 200px" />
          </a-form-item>
          <a-form-item label="累计车贴余额">
            <a-input-number :max="99999999" :min="0"  v-model.number="oldCarSubsidyBalance" placeholder="累计车贴余额" disabled  style="width: 200px" />
          </a-form-item>
        </a-form>
        <a-form layout="inline" style="margin-top: 20px;">
          <a-form-item label="新油卡卡号" required>
            <SelectOilCardNumberInput ref="newCard"  v-model="oldNumber" style="width: 200px" :status="'1'"  :multi="false" @back="newOilbackHandle"></SelectOilCardNumberInput>
          </a-form-item>
          <a-form-item label="截止当前累计自驾补助余额">
            <a-input-number :max="99999999" :min="0"  v-model.number="newOilCardDrivingSubsidyBalance" disabled placeholder="截止当前累计自驾补助余额"  style="width: 200px" />
          </a-form-item>
          <a-form-item label="累计车贴余额">
            <a-input-number :max="99999999" :min="0"  v-model.number="newCarSubsidyBalance" disabled placeholder="累计车贴余额"  style="width: 200px" />
          </a-form-item>
        </a-form>
      
    </a-modal>
    <erp-oil-card-manager-modal ref="modalForm" @ok="modalFormOk"></erp-oil-card-manager-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ErpOilCardManagerModal from './modules/ErpOilCardManagerModal'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  import SelectOilCardNumberInput from '@/views/scy/components/SelectOilCardNumberInput'
   import { getAction, httpAction } from '@/api/manage'
// import { postAction } from '../../../../../api/manage'
import { postAction } from '@/api/manage'

  export default {
    name: 'ErpOilCardManagerList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      ErpOilCardManagerModal,
      SelectUserByDep,
      SelectOilCardNumberInput
    },
    data () {
      return {
        description: '油补维护表管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'油卡卡号',
            align:"center",
            width:200,
            dataIndex: 'cardNumber',
            ellipsis: true
          },
          {
            title:'经办人',
            align:"center",
            width:100,
            dataIndex: 'agent_dictText',
            ellipsis: true
          },
          {
            title:'领用人',
            align:"center",
            width:100,
            dataIndex: 'cardRecipient_dictText',
            ellipsis: true
          },
          // {
          //   title:'领用人所属部门',
          //   align:"center",
          //   dataIndex: 'cardRecipientDeptId_dictText',
          //   ellipsis: true
          // },
          {
            title:'领用人所属部门',
            align:"center",
            width:250,
            dataIndex: 'rdeptName',
            ellipsis: true
          },
          {
            title:'油卡开户公司',
            align:"center",
            width:200,
            dataIndex: 'openCompany_dictText',
            ellipsis: true
          },
          {
            title:'状态',
            align:"center",
            width:100,
            dataIndex: 'cardStatus_dictText',
            ellipsis: true
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark',
            ellipsis: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: {
            filterDropdown: 'filterDropdown',
            filterIcon: 'filterIcon',
            customRender: 'action' }
          }
        ],
        url: {
          list: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/list",
          delete: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/delete",
          deleteBatch: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/deleteBatch",
          exportXlsUrl: "/api/erp/modules/workbench/oilSubsidy/erpOilCardManager/exportXls",
          importExcelUrl: "api/erp/modules/workbench/oilSubsidy/erpOilCardManager/importExcel",
          queryByUserId: "/api/erp/modules/workbench/oilSubsidy/erpMyOilSubsidy/queryByUserIdNew",
          //金额转移
          editMoney:"/api/erp/modules/workbench/oilSubsidy/erpMyOilSubsidy/editMoney"
        },
        dictOptions:{},
        superFieldList:[],
        modalvisible: false,
        //旧油卡自驾补助余额
        oldOilCardDrivingSubsidyBalance: 0,
        //旧油卡车贴余额
        oldCarSubsidyBalance: 0,
        //新油卡自驾补助余额
        newOilCardDrivingSubsidyBalance:0,
        //新油卡车贴余额
        newCarSubsidyBalance: 0,
        // //旧油卡卡号
        // oldCardNumber: '',
        // //新油卡卡号
        // newCardNumber: ''
        //油卡转移对象
        setModel: {},
        newNumber: '',
        oldNumber: ''
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleEdit: function (record) {
        //debugger;
        let disableOther = false
        this.$refs.modalForm.edit(record,disableOther)
        this.$refs.modalForm.title = '编辑'
        this.$refs.modalForm.disableCardRecipient = true
        this.$refs.modalForm.disableSubmit = false
      },
      handleAdd: function () {
        let disableOther = true
        this.$refs.modalForm.add(disableOther)
        this.$refs.modalForm.title = '新增'
        this.$refs.modalForm.disableSubmit = false
        this.$refs.modalForm.disableCardRecipient = false
      },

      handleCancel(){
        this.modalvisible = false
        this.oldOilCardDrivingSubsidyBalance = 0
        this.oldCarSubsidyBalance = 0
        this.newOilCardDrivingSubsidyBalance = 0
        this.newCarSubsidyBalance = 0
        this.newNumber = ''
        this.oldNumber = ''
        // this.$set(this.setModel,'newProposer',undefined)
        // this.$set(this.setModel,'newCardNumber',undefined)
        // this.$set(this.setModel,'oldProposer',undefined)
        // this.$set(this.setModel,'oldCardNumber',undefined)
      },
      //转移按钮
      handleTransfer(record){
        this.modalvisible = true
        // this.$refs.oldCard.cardRecipientHandle(record.cardRecipient)
        // this.$refs.oldCard.cardRecipientHandle(record.cardRecipient)
         this.$nextTick(()=>{
          // console.log('滴滴滴滴滴滴多：',this.$refs.oldCard);
          this.$refs.oldCard.cardRecipientHandle(record.cardRecipient)
          this.$refs.newCard.cardRecipientHandle(record.cardRecipient)
        })
        
      },
      // 老油卡选择回调 带出两个余额
      OldOilbackHandle(data){
        //添加油卡转移必要信息
            this.$set(this.setModel,'oldProposer',data[0].cardRecipient)
            this.$set(this.setModel,'oldCardNumber',data[0].id)
        getAction(this.url.queryByUserId,{userId: data[0].cardRecipient,cardNumber: data[0].id}).then(res => {
          if(res.success){
            this.oldCardNumber = res.result.cardNumber
            this.oldOilCardDrivingSubsidyBalance = res.result.drivingSubsidyBalance
            this.oldCarSubsidyBalance = res.result.carSubsidyBalance
          }else{
            this.$message.warning("查询失败!")
          }
        })
      },
      //新油卡选择回调 带出两个余额
      newOilbackHandle(data){
        //添加油卡转移必要信息
        this.$set(this.setModel,'newProposer',data[0].cardRecipient)
        this.$set(this.setModel,'newCardNumber',data[0].id)
        getAction(this.url.queryByUserId,{userId: data[0].cardRecipient,cardNumber: data[0].id}).then(res => {
          if(res.success){
            this.newCardNumber = res.result.cardNumber
            this.newOilCardDrivingSubsidyBalance = res.result.drivingSubsidyBalance
            this.newCarSubsidyBalance = res.result.carSubsidyBalance
          }else{
            this.$message.warning("查询失败!")
          }
        })
      },
      // 转移弹窗弹框确认按钮
      handleOKk(){
        //最终截至累计自驾补助余额 新 + 旧
        let cardDrivingSubsidyBalance =  this.newOilCardDrivingSubsidyBalance + this.oldOilCardDrivingSubsidyBalance;
        //截止当前累计车贴余额 新 + 旧
        let carSubsidyBalance = this.newCarSubsidyBalance + this.oldCarSubsidyBalance

        //设置油卡金额转移所需要的必要字段
        this.$set(this.setModel,'drivingSubsidyBalance',cardDrivingSubsidyBalance)
        this.$set(this.setModel,'carSubsidyBalance',carSubsidyBalance)

        if(this.setModel.oldCardNumber == null || this.setModel.oldCardNumber == undefined || this.setModel.oldCardNumber == ''){
          this.$message.warning('请选择旧油卡卡号！');
          return
        }
        if(this.setModel.newCardNumber == null || this.setModel.newCardNumber == undefined || this.setModel.newCardNumber == ''){
          this.$message.warning('请选择油卡卡号！');
          return
        }
        // 更新新卡余额
        postAction(this.url.editMoney,this.setModel).then(res => {
              if(res.success){
                this.$message.success(res.message);
                this.modalvisible = false
                this.loadData(1);
              }else{
                this.$message.warning(res.message);
                this.modalvisible =  false
              }
            })

        this.oldOilCardDrivingSubsidyBalance = 0
        this.oldCarSubsidyBalance = 0
        this.newOilCardDrivingSubsidyBalance = 0
        this.newCarSubsidyBalance = 0
        this.newNumber = ''
        this.oldNumber = ''
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'cardNumber',text:'油卡卡号',dictCode:''})
        fieldList.push({type:'string',value:'agent',text:'经办人',dictCode:''})
        fieldList.push({type:'string',value:'cardRecipient',text:'领用人',dictCode:''})
        fieldList.push({type:'string',value:'openCompany',text:'油卡开户公司',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
